﻿using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.Title = ConfigurationManager.AppSettings["WebSiteName"];

        lblloginStat.Visible = false;
        lnkLogout.Visible = false;
        pnllogin.Visible = true;
        lnkSignup.Visible = true;

        if (this.Page.User.Identity.Name != null && this.Page.User.Identity.Name != "" && AppUser.currUser() > 0)
        {
            lblloginStat.Text = string.Format("Welcome {0},", this.Page.User.Identity.Name);
            lblloginStat.Visible = true;

            lnkLogout.Visible = true;
            pnllogin.Visible = false;
            lnkSignup.Visible = false;
        }

        tdRepeater.Attributes["background"] = Page.ResolveClientUrl("~/images/search_bar/search_bar_repeater.png");
    }

    protected void btnSearch_click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("~/UI/TrailList.aspx?keywords={0}", txtSearch.Text.Replace(" ", "+")));
    }

    protected void btnLogin_click(object sender, EventArgs e)
    {
        Bintje.bintje.imageoSession currentSession;

        /* TODO: Check the way login works */

        if (BintjeMembershipProvider.ValidateUser(txtUsername.Text.Trim(), txtPassword.Text.Trim(), out currentSession))
        {
            FormsAuthentication.SetAuthCookie(txtUsername.Text.Trim(), true);
            ltrLoginMessage.Visible = false;
            lblloginStat.Text = string.Format("Welcome {0},", txtUsername.Text.Trim());
            lblloginStat.Visible = true;
            lnkLogout.Visible = true;

            pnllogin.Visible = false;
            lnkSignup.Visible = false;

            Session["UserId"] = currentSession.userId;
            Session["Session"] = currentSession;

            /*
            dbTrailInfoDataContext dbo = BL.dbTrail;
            var usr = (from u in dbo.UserDetails
                       where u.username == txtUsername.Text.Trim()
                       select new { u.ID }).ToArray();
            if (usr.Length > 0)
            {
                Session["UserId"] = usr[0].ID;
            }
            Response.Redirect("~/UI/Home.aspx");
            */
            //Response.Redirect(Request.Url.ToString());

            Response.Redirect("~/UI/Home.aspx");
        }
        else
        {
            ltrLoginMessage.Visible = true;
            pnllogin.Visible = true;
            lnkSignup.Visible = true;
            Response.Redirect("~/Login.aspx");
        }
    }

    protected void lsGeo_logout(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Bintje.bintje.imageoSession ses = Session["Session"] as Bintje.bintje.imageoSession;
        if (null != ses)
        {
            try
            {
                Bintje.User user = new Bintje.User(ses);
                user.doLogout();
                Session["Session"] = null;
            }
            catch { }

        }
        HttpContext.Current.Session.Abandon();
        Session["UserId"] = "";
        lblloginStat.Visible = false;
        lnkLogout.Visible = false;
        pnllogin.Visible = true;
        lnkSignup.Visible = true;
        Response.Redirect("~/UI/Home.aspx");
    }
}
